﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using USERS.CYT369.COM.Models;
using USERS.CYT369.COM.MODELS.Brand;

namespace USERS.CYT369.COM.Controllers
{
    public partial class BrandController : AuthorizeController
    {
        [HttpGet]
        [HttpPost]
        public IActionResult BrandIndex()
        {
            var status = Request.Query.GetInteger("status");

            return GetBrands("BrandIndex", status);
        }
        public IActionResult BrandAuditPassed()
        {
            return GetBrands("BrandIndex", 1);
        }

        public IActionResult BrandAuditFailed()
        {
            return GetBrands("BrandIndex", -1);
        }
        public IActionResult BrandAuditAwait()
        {
            return GetBrands("BrandIndex", 0);
        }
        public IActionResult BrandDraft()
        {
            return GetBrands("BrandIndex", -2);
        }

        public IActionResult BrandDel()
        {
            return GetBrands("BrandDel", null, true);
        }

        private IActionResult GetBrands(string view, int? status, bool deleted = false)
        {
            int? year = null;
            int? month = null;
            int? categoryID = null;
            string name = null;

            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 10);
            int count = 0;

            if (Request.HasFormContentType)
            {
                year = Request.Form.GetInteger("docYears");
                month = Request.Form.GetInteger("docMonths");
                categoryID = Request.Form.GetInteger("categoryID");
                name = Request.Form.GetString("search-key");
            }

            var brands = USERS.CYT369.COM.DAL.BrandService.Instance.GetBrands(page, rows, ref count, this.GetUserId(), status, name, year, month, categoryID);
            var allCitys= USERS.CYT369.COM.DAL.CityService.Instance.GetCitys();
            //城市字典
            var citys = new Dictionary<int?, USERS.CYT369.COM.MODELS.Base.City>();
            if (allCitys != null)
            {
                foreach (var c in allCitys)
                {
                    citys[c.Id] = c;
                }
            }
            ViewBag.citys = citys;
            ViewBag.Stastics = USERS.CYT369.COM.DAL.BrandService.Instance.GetBrandStastics(this.GetUserId());
            ViewBag.UserID = this.GetUserId();
            ViewBag.SearchYear = year;
            ViewBag.SearchMonth = month;
            ViewBag.SearchCategoryID = categoryID;
            ViewBag.SearchName = name;

            ViewBag.PageNumber = page;
            ViewBag.PageRows = rows;
            ViewBag.TotalCount = count;

            return View(view, brands);
        }
        public IActionResult BrandEdit(string id)
        {
            int userId = this.GetUserId();
            USERS.CYT369.COM.MODELS.Brand.Brand brand = null;
            if (!string.IsNullOrEmpty(id))
            {
                var argsJson = Base64.Base64ToJSON(id);
                long? brandId = argsJson.GetInteger("ID");
                if (brandId.HasValue)
                {
                    brand = USERS.CYT369.COM.DAL.BrandService.Instance.GetBrandByID_IncludeImg(brandId.Value);
                }
            }
            ViewBag.UserID = userId;
            ViewBag.Citys = USERS.CYT369.COM.DAL.CityService.Instance.GetCitys();
            ViewBag.CategoryParents = USERS.CYT369.COM.DAL.BrandService.Instance.CategorysGetByParentID(0);
            return View("BrandEdit", brand);
        }

    }
}
